package com.hans.scala

object ListExample {
  def main(args: Array[String]) {
    val l = List(1, 2, 3, 4)
    def g(v: Int) = List(v - 1, v, v + 1)
    val t1 = l.map(x => g(x))
    println(t1) //List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5))
    val t2 = l.flatMap(x => g(x))
    //直接地
    println(t2) //List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5)
    println("list map:")
    List(41, "cat") foreach {
      case i: Int => println(i + 1)
      case c: String => println(c)
    }
    println("list collect:")
    List(41, "cat") collect {
      case i: Int => println(i + 1)
    }

  }
}
